/*
 * @lc app=leetcode.cn id=387 lang=javascript
 *
 * [387] 字符串中的第一个唯一字符
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var firstUniqChar = function (s) {
  const map = new Map();
  let index = -1;

  for (let i = 0; i < s.length; i++) {
    if (map.has(s[i])) {
      map.set(s[i], map.get(s[i]) + 1);
    } else {
      map.set(s[i], 1);
    }
  }

  for (key of map.keys()) {
    if (map.get(key) === 1) {
      index = s.indexOf(key);
      break;
    }
  }

  return index;
};
// @lc code=end

// @after-stub-for-debug-begin
module.exports = firstUniqChar;
// @after-stub-for-debug-end
